package com.ghostflying.wlanassistant;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class WifiAutoLoginFragment extends Fragment {
	
/*	private String username = null;
	private String password = null;
	private Boolean forceLogin = false;*/
	private EditText input_username = null;
	private EditText input_password = null;
	private CheckBox input_force_login = null;
	private Button save = null;
	
	public static final String PARAM_STORE = "ZJUWLAN";
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState){
		View rootView = null;
		rootView = inflater.inflate(R.layout.fragment_wifi_autologin, container,false);
		input_username = (EditText)rootView.findViewById(R.id.username);
		input_password = (EditText)rootView.findViewById(R.id.password);
		input_force_login = (CheckBox)rootView.findViewById(R.id.forcelogin);			
		save = (Button)rootView.findViewById(R.id.save);
		save.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				storeZJUWLANInfo();
			}
			
		});
		getZJUWLANInfo ();
		return rootView;
	}
	
	private void storeZJUWLANInfo(){
		Editor medit = getActivity().getSharedPreferences(PARAM_STORE, MainActivity.MODE_PRIVATE).edit();
		medit.putString("USERNAME", input_username.getText().toString().trim());
		medit.putString("PASSWORD", input_password.getText().toString().trim());
		medit.putBoolean("FORCELIGIN", input_force_login.isChecked());
		medit.commit();
		Toast.makeText(getActivity(), R.string.zjuwlan_stored, Toast.LENGTH_SHORT).show();
	}
	
	private Boolean getZJUWLANInfo (){
		SharedPreferences settings = getActivity().getSharedPreferences(PARAM_STORE, MainActivity.MODE_PRIVATE);
		input_username.setText(settings.getString("USERNAME", ""));
		input_password.setText(settings.getString("PASSWORD", ""));
		input_force_login.setChecked(settings.getBoolean("FORCELOGIN", false));
		if (input_username.getText().toString().trim().equals("")){
			return false;
		}
		return true;
	}

}
